

//调用
$(document).ready(function(){
    $('#password_form').bind('submit', function(){
        ajaxSubmit(this, function(data){

            console.log(JSON.stringify(data));

            var res = data;
            if(res.success) // 成功
            {
                $('#res_info').html("您的密码重置成功。");
                $('.regist-wrap-dialog').css("display", "flex");
            }
            else {
                // 显示错误信息
                //alert(res.message);

                $('#res_info').html(res.message);
                $('.regist-wrap-dialog').css("display", "flex");

            }
            //

        }, function (err) {
            alert(err);
        });
        return false;
    });
});





//将form转为AJAX提交
function ajaxSubmit(frm, fn, err) {
    var dataPara = getFormJson(frm);

    dataPara.new_login_password = hex_md5(dataPara.new_login_password);
    dataPara.new_login_password2 = hex_md5(dataPara.new_login_password2);
    console.log(dataPara);

    if(dataPara.new_login_password != dataPara.new_login_password2)
    {
        $('#res_info').html('两次输入的密码不同！');
        $('.regist-wrap-dialog').css("display", "flex");
    }
    else
    {
        $.ajax({
            url: frm.action,
            xhrFields:{withCredentials:true},
            type: frm.method,
            data: dataPara,
            success: fn,
            error : err
        });
    }
}

//将form中的值转换为键值对。
function getFormJson(frm) {
    var o = {};
    var a = $(frm).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });

    return o;
}

function send_verify_code(obj) {

    var login_id = $("#login_id").val();
    if(login_id == "")
    {
        $('#res_info').html('用户名不能为空');
        $('.regist-wrap-dialog').css("display", "flex");
        return ;
    }

    $.ajax({
        url: "http://47.104.102.188:7979/user/email_verify_code/send",
        xhrFields:{withCredentials:true},
        type: "post",
        data: {
            login_id:login_id,
            code_type:"RESET_PASSWORD",
        },
        success: function(data)
        {
            console.log(JSON.stringify(data));
            if(data.success)
            {
                $('#res_info').html('验证码已发送到邮箱。<br> 验证码有效时间为10分钟。');
                $('.regist-wrap-dialog').css("display", "flex");
            }
            else
            {
                $('#res_info').html(data.message);
                $('.regist-wrap-dialog').css("display", "flex");
            }
        },
        error : function(err)
        {
            console.log("验证码 error");
        }
    });
}

function closeDialog()
{
    $('.regist-wrap-dialog').css("display", "none");
}


